<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Db;


class Article extends Controller
{
    public function index($aid)
    {
        //栏目条
        if (session('uid', '', 'think')) {
            $ass = Db::name('subscribe s')
                ->join('column c', 's.cid = c.cid')
                ->where('s.uid =' . session('uid', '', 'think'))
                ->order('id desc')
                ->limit(10)
                ->select();

        } else {
            $ass = null;
        }
        //登录栏目条
        if (session('uid','','think')) {  
        $assz = Db::name('subscribe s')
        ->join('column c','s.cid = c.cid')
        ->where('s.uid ='.session('uid','','think'))
        ->select();
            
        } else {
           $assz = null;
        }
        //内容
        $content = Db::name('article a')
            ->join('article_func f', 'f.aid = a.aid')
            ->join('user_self u', 'u.uid = a.uid')
            ->where('a.aid = ' . $aid)
            ->find();
        //查询已收藏的会员
        $souca = Db::name('house')->field('uid')->where('aid ='.$aid)->find();
        if (!empty($souca)) {
        $soucag = array_values($souca);
        } else {
        $soucag = array();

        }
        //已经关注了的
        if (session('uid', '', 'think')) {
            $att = Db::name('follow f')
                ->where('f.uid =' . session('uid', '', 'think'))
                ->field('buid')
                ->select();
            $att2 = array();
            foreach ($att as $key => $v) {
                $att2[] = $v['buid'];
            }

            $att3 = null;
            foreach ($att2 as $key => $value) {
                $att3 .= $value . ',';
            }
        } else {
            $att3 = null;
        }
        $hots = Db::name('column')
            ->where('pid > 0 and hot = 1')
            ->field('cid')
            ->select();

        //未登入栏目
        $hotss = Db::name('column')
        ->where('pid > 0 and hot = 1')
        ->limit(9)
        ->select();

        //未登入下栏目
        $hotssz = Db::name('column')
        ->where('pid > 0 and hot = 1')
        ->limit(10)
        ->select();

        //订阅
        if (session('uid', '', 'think')) {
            $list = Db::name('subscribe s')
                ->where('s.uid =' . session('uid', '', 'think'))
                ->field('cid')
                ->select();
            $list2 = array();
            foreach ($list as $key => $v) {
                $list2[] = $v['cid'];
            }

            $list3 = null;
            foreach ($list2 as $key => $value) {
                $list3 .= $value . ',';
            }
        } else {
            $list3 = null;
        }


        //随机频道
        $arti = Db::query('SELECT * FROM `column` where pid > 0 ORDER BY RAND() LIMIT 6 ');

        // var_dump($content);die;
        //作者
        $colu = Db::name('article a')
            ->join('column c', 'c.cid = a.cid')
            ->where('a.aid = ' . $aid)
            ->select();

        //评论
        $discusse = Db::table(['user_self','discuss'])
            ->field('discuss.time,discuss.content,discuss.did,user_self.nickname')
            ->where('user_self.uid=discuss.uid and discuss.aid='. $aid)
            ->order('time desc')
            ->select();

        foreach ($discusse as $k=>$v){
            $reply = Db::table(['answer','user_self'])->field('user_self.nickname,answer.contente')->where('user_self.uid=answer.uid and did ='.$v['did'])->select();
            $discusse[$k]['answers'] = $reply;
        }
//         var_dump($discusse);die;
        return view('Article/index', [
            'ass' => $ass,
            'assz' => $assz,
            'content' => $content,
            'list3' => $list3,
            'att3' => $att3,
            'hots' => $hots,
            'hotss' => $hotss,
            'arti' => $arti,
            'hotssz' => $hotssz,
            'soucag' =>$soucag,
            'discusse' =>$discusse,
        ]);
    }

    //关注
    public function follow($buid)
    {
        // return $buid;
        $uid = session('uid', '', 'think');
        if ($buid == $uid) {
            return 0;
        } else {

        $data = ['uid' => $uid, 'buid' => $buid, 'time' => time()];
        // var_dump($data);die
        $list = Db::name('follow')->insert($data);
        return json($list);
        }
    }

    //取消关注
    public function cancel($buid)
    {
        $uid = session('uid', '', 'think');
        $data = ['uid' => $uid, 'buid' => $buid];
        // return $data;
        $list = Db::name('follow')->where($data)->delete();
        return json($list);
    }

    

}